//
//  SearshBcTradeRecordVC.m
//  NtoccDriverNew
//
//  Created by mac on 2021/3/11.
//  Copyright © 2021 GCY. All rights reserved.
//

#import "SearshBcTradeRecordVC.h"
#import "STPickerView.h"
#import "STPickerSingle.h"

@interface SearshBcTradeRecordVC ()<STPickerSingleDelegate>
{
    NSString *_startDate;
    NSString *_endDate;
    NSString *_fylxStr;
}
@end

@implementation SearshBcTradeRecordVC

static const CGFloat itemHeight = 45;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _startDate = @"";
    _endDate = @"";
    _fylxStr = @"";
    
    self.navigationItem.title = @"记录搜索";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self setupBaseView];
    
}

- (void)setupBaseView {
    
    NSArray *formsTitleArr = @[@"运输单号", @"费用类型", @"开始时间", @"截止时间"];
    NSArray *placeholderArr = @[@"请填写运输单号", @"请选择", @"请选择开始时间", @"请选择截止时间"];
    
    for (int i=0; i<formsTitleArr.count; i++) {
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 1 + itemHeight*i, ScreenW, itemHeight)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bgView];
        
        //输入框
        if (i==0) {
            
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenW-215, 0, 200, itemHeight)];
            textField.tag = 10 + i;
            textField.font = font(14);
            textField.text = @"";
            textField.textColor = Dark_TextColor;
            textField.textAlignment = NSTextAlignmentRight;
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderArr[i] attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
            [bgView addSubview:textField];
            
        } else {
            //按钮
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(ScreenW-230, 0, 200, itemHeight);
            button.tag = 10 + i;
            [button setTitle:placeholderArr[i] forState:UIControlStateNormal];
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            button.titleLabel.font = font(14);
            [button setTitleColor:Light_TextColor forState:UIControlStateNormal];
            [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
            [bgView addSubview:button];
            
            UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenW-20, 17, 6, 11)];
            arrow.image = image(@"gcy_icon_hd_rarrow");
            [bgView addSubview:arrow];
        }
        
        //标题
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 120, itemHeight)];
        textLabel.text = formsTitleArr[i];
        textLabel.textColor = Dark_TextColor;
        textLabel.font = font(14);
        textLabel.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:textLabel];
        
        //分割线
        UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, itemHeight-1, ScreenW - 30, 1)];
        hLine.backgroundColor = BackgroundColor;
        [bgView addSubview:hLine];
    }
    
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn setBackgroundColor:Theme_ColorValue];
    cut(searchBtn, BTN_BORDER_RADIUS);
    searchBtn.tag = 100;
    [searchBtn addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(20, 4 * 45 + 20, ScreenW-40, 45);
    [self.view addSubview:searchBtn];
}

- (void)btnEventClick:(UIButton *)button {
    
    if (button.tag == 11) {
        NSArray *dataArr = @[@"运费", @"预付"];
        STPickerSingle *fylxPickerSingle = [[STPickerSingle alloc]init];
        fylxPickerSingle.widthPickerComponent = ScreenW;
        [fylxPickerSingle setArrayData:(NSMutableArray *)dataArr];
        [fylxPickerSingle setTitle:@"请选择费用类型"];
        [fylxPickerSingle setContentMode:STPickerContentModeBottom];
        [fylxPickerSingle setDelegate:self];
        [fylxPickerSingle show];
        
    } else if (button.tag == 12) {
        //开始时间
        BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMDHMS];
        datePickerView.title = @"选择开始时间";
        datePickerView.isAutoSelect = NO;
        datePickerView.showToday = YES; // 是否显示“今天”
        datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
            [button setTitle:selectValue forState:UIControlStateNormal];
            _startDate = selectValue;
        };
        [datePickerView show:self];
        
    } else if (button.tag == 13) {
        //截止时间
        BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMDHMS];
        datePickerView.title = @"选择截止时间";
        datePickerView.isAutoSelect = NO;
        datePickerView.showToday = YES; // 是否显示“今天”
        datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
            [button setTitle:selectValue forState:UIControlStateNormal];
            _endDate = selectValue;
        };
        [datePickerView show:self];
        
    } else {
        
        //搜索
        UITextField *ysdhTF = [self.view viewWithTag:10];

        if (self.SearchResultBlock) {
            self.SearchResultBlock(ysdhTF.text, _fylxStr, _startDate, _endDate);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
#pragma mark -- STPickerSingleDelegate
- (void)pickerSingle:(STPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle {
    UIButton *btn = [self.view viewWithTag:11];
    [btn setTitle:selectedTitle forState:UIControlStateNormal];
    _fylxStr = [selectedTitle isEqual:@"运费"] ? @"0" : @"1";
}

@end
